let express=require('express');
let router = express.Router();




//{employee：{num,pages:{contentid,content,title}}}//posted

router.get('/', function(req, res, next) {

  const r=[];
  //const ps=[]
  const modulename=['employee','analysis','attendance','insurance','process','salary','setting']
  var pool = global.POOL;
  var query = async () => {
    var connect = await pool.connect()
    try {
      for(const m_name of modulename)
      {
        const result=await connect.query('SELECT id AS contentid,title,posted FROM contents WHERE module_name=$1 and deleted=$2', [m_name,false])
        r.push({modulename:m_name,pages:result.rows});
      }
      
      res.send(r);
    } finally {
      connect.release()
    }
  }

  query().catch(e => {
    res.send({getMes:'error'});
    console.error(e.message, e.stack)});
});
  /*const modulename=['employee','analysis','attendance','insurance','process','salary','setting']
  for(let i=0;i<modulename.length;i++)
    sqls.push( `select id,title,posted from contents where module_name = '${modulename[i]}' and deleted=0`)
    
  for(sql of sqls)
  {
    ps.push(new Promise(
      (resolve,reject)=>{
        connection.query(sql,(err,rows,fields)=>{
            if(err)
            {
              reject([]);
            }
            resolve(rows);
        })
      }
    ))
  }
  
  const p=Promise.all(ps);
  p.then((out)=>{
    let result=[];
    for(let i=0;i<out.length;i++)
    {
      result.push({modulename:modulename[i],pages:out[i]})
    }
    res.send(result)
  })

})*/
module.exports = router;
